package com.xywh.dakaqiandao.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xw.repo.XEditText;
import com.xywh.dakaqiandao.R;
import com.xywh.dakaqiandao.di.component.DaggerForgotPasswordComponent;
import com.xywh.dakaqiandao.di.module.ForgotPasswordModule;
import com.xywh.dakaqiandao.mvp.contract.ForgotPasswordContract;
import com.xywh.dakaqiandao.mvp.presenter.ForgotPasswordPresenter;
import com.xywh.dakaqiandao.mvp.ui.entity.ForgotPassword;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.utils.Preconditions.checkNotNull;
import static com.jess.arms.base.BaseConstants.BASE_URL;
import static com.jess.arms.base.BaseConstants.NAME_SPACE;


@Route(path = RouterHub.ACTIVITY_FORGOT_PASSWORD)
public class ForgotPasswordActivity extends BaseActivity<ForgotPasswordPresenter> implements ForgotPasswordContract.View {
    @BindView(R.id.et_mi_ma)
    XEditText etMiMa;
    @BindView(R.id.et_que_ren_mi_ma)
    XEditText etQueRenMiMa;
    @BindView(R.id.et_shen_fen_zheng_hao)
    XEditText etShenFenZhengHao;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerForgotPasswordComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .forgotPasswordModule(new ForgotPasswordModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_forgot_password; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_forgot_password));
//        getForgotPasswordData();
    }

    private void getForgotPasswordData() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getForgotPasswordData(map);
    }

    @Override
    public void getForgotPasswordDataSuccess(ForgotPassword entityData) {


    }

    @OnClick({R.id.login_btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.login_btn_login:
                if (TextUtils.isEmpty(getShenFenZhengHao())) {
                    ArmsUtils.makeText(getContext(),"请输入身份证号");
                    return;
                }
                if (TextUtils.isEmpty(getMiMa())) {
                    ArmsUtils.makeText(getContext(),"请输入密码");
                    return;
                }
                if (TextUtils.isEmpty(getQueRenMiMa())) {
                    ArmsUtils.makeText(getContext(),"请输入确认密码");
                    return;
                }
                if (!getMiMa().equals(getQueRenMiMa())) {
                    ArmsUtils.makeText(getContext(),"两次密码输入不一致");
                    return;
                }


                UpdateRegisterPass();

                break;
        }
    }

    @NonNull
    private String getShenFenZhengHao() {
        return etShenFenZhengHao.getText().toString().trim();
    }

    @NonNull
    private String getMiMa() {
        return etMiMa.getText().toString().trim();
    }

    @NonNull
    private String getQueRenMiMa() {
        return etQueRenMiMa.getText().toString().trim();
    }

    /**
     * public string UpdateRegisterPass(string identityCardNo, string employeePassword)
     *
     * @param
     */
    private void UpdateRegisterPass() {

        showLoading();

        String methodName = "UpdateRegisterPass";

        SoapUtil soapUtil = SoapUtil.getInstance(this);
        soapUtil.setDotNet(true);
        SoapParams params = new SoapParams();
        params.put("identityCardNo", getShenFenZhengHao());
        params.put("employeePassword", getMiMa());


        /**
         * 请求
         */
        soapUtil.callEntry(BASE_URL, NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty("UpdateRegisterPassResult").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("-1")) {
                    ArmsUtils.makeText(getContext(), "密码修改成功");
                    killMyself();
                    return;
                } else {
                    ArmsUtils.makeText(getContext(), "密码修改失败");
                    return;
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
            }
        },getAapiUserName(),getApiPwd());


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}
